﻿using Symmetry.PluginSupport;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace Plugins1
{
    public sealed partial class MVVMInspector : UserControl, ICustomEditor, IPropertyGridHost
    {
        public MVVMInspector()
        {
            this.InitializeComponent();
            Loaded += MVVMInspector_Loaded;
        }

        void MVVMInspector_Loaded(object sender, RoutedEventArgs e)
        {
            //Get the application and find view model.
            try
            {
                if (Application.Current.Resources.ContainsKey("ViewModelLocator"))
                {
                    _target = Application.Current.Resources["ViewModelLocator"];
                    UpdateTargetOnHost();
                }
                else
                {
                    RenderInstructions();
                }
            }
            catch (Exception)
            {


            }
        }

        public string Description
        {
            get
            {
                return @"MVVM Inspector is a Symmetry Plugin for manipulating MVVM Pattern assets.  MVVM Inspector hooks to your view model locator to give you visibility into and control over your models and views ant run-time.";
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string FriendlyName
        {
            get
            {
                return "MVVM Inspector";
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        object _target = null;

        public void SetObject(object target)
        {
              
        }

        /// <summary>
        /// Tell prop grid to use the current viewmodellocator as its selected object.
        /// </summary>
        private void UpdateTargetOnHost()
        {
            pgVM.SetObject(_target);
        }

        /// <summary>
        /// Shows instructions content.
        /// </summary>
        private void RenderInstructions()
        {            
            VisualStateManager.GoToState(this, "instructions", true);
        }

        public string UniqueID
        {
            get { return "63E7F373-EE3E-404C-8778-3384AD4C46D5"; }
        }


        IPropertyGrid pgVM = null;
        public IPropertyGrid PropGrid
        {
            get
            {
                return pgVM;
            }
            set
            {
                pgVM = value;
                //Updat ui.
                if (Normal != null)
                {
                    Normal.Children.Add(value as FrameworkElement);
                }
            }
        }
    }
}
